package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MTagService;
import com.wicket.okrframework.base.service.dto.req.QueryTagDetailReq;
import com.wicket.okrframework.base.service.dto.req.QueryTagListReq;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.TagMapper;
import com.wicket.okrframework.dal.po.mbg.StructureStationGrade;
import com.wicket.okrframework.dal.po.mbg.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MTagServiceImpl
        extends ServiceImpl<TagMapper, com.wicket.okrframework.dal.po.mbg.Tag>
        implements MTagService {

    @Autowired
    TagMapper tagMapper;

    /**
   * 1-1-16查询标签详情[203]
   * gen by moon at 12/16/2022, 3:08:53 PM
   */
 @Trace(operationName = "1-1-16查询标签详情")
 @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数",value = "arg[0]"),
 @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  Tag  queryTagDetail(QueryTagDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<Tag> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                Tag retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-1-16查询标签列表[112]
   * gen by moon at 10/25/2022, 9:51:16 AM
   */
 @Trace(operationName = "1-1-16查询标签列表")
 @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数",value = "arg[0]"),
 @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<Tag>  queryTagList(QueryTagListReq reqDto) {
     
        try {
            QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(Tag.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(Tag.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(Tag.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<Tag>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-1-16删除标签[111]
   * gen by moon at 8/17/2022, 8:00:07 PM
   */
 @Trace(operationName = "1-1-16删除标签")
 @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数",value = "arg[0]"),
 @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteTag(String tagId ) {
     
      return super.removeById(tagId);
  }

    /**
   * 1-1-16修改标签[110]
   * gen by moon at 8/17/2022, 8:00:06 PM
   */
 @Trace(operationName = "1-1-16修改标签")
 @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数",value = "arg[0]"),
 @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateTag(Tag tag) {
     tag.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(tag!=null && tag.getSubjectLifeCycle()!=null &&  tag.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            tag.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(tag!=null && tag.getSubjectLifeCycle()!=null && tag.getSubjectLifeCycle().equals("ARCHIVING")) {
              tag.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
          }
tag.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(tag!=null && tag.getSubjectLifeCycle()!=null && tag.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             tag.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
if(tag!=null && tag.getSubjectLifeCycle()!=null && tag.getSubjectLifeCycle().equals("ARCHIVING")) {
              tag.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
          }
        return super.updateById(tag);
  }

    /**
   * 1-1-16新增标签[109]
   * gen by moon at 8/17/2022, 8:00:06 PM
   */
 @Trace(operationName = "1-1-16新增标签")
 @Tags({@org.apache.skywalking.apm.toolkit.trace.Tag(key = "参数",value = "arg[0]"),
 @org.apache.skywalking.apm.toolkit.trace.Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addTag(Tag tag) {
     tag.setTagId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
tag.setIsArchive("FALSE");
tag.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
tag.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
tag.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
tag.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
tag.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
tag.setReleaseTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
tag.setIsValid("FALSE");
tag.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
tag.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(tag)){
        return tag.getTagId();
     }else {
        return "-1";
     }
    
  }
}
